﻿Imports System.Data.SqlClient
Imports System.Data

'All the stuff we're using from import.

Public Class WebForm1
    Inherits System.Web.UI.Page
    Dim con As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Our connection string to the database:

        con.ConnectionString = "Data Source=.\watafa0;Initial Catalog=CSTestDB2;Integrated Security=True"
        'This is my database on my computer, so you'll need to alter this to whatever your database and server name are.

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        'Create a button. Open the connection on the button click and add a command to the data adapter. Execute it with executeNonQuery, and then fill the dataadapter with the results.
        'Then populate the gridview with all the information. Kind of hard to explain, I'm not sure I fully understand it to be honest.

        con.Open()
        da = New SqlDataAdapter("Select * from tblUsers", con)
        da.SelectCommand.ExecuteNonQuery()
        da.Fill(ds, 0)
        GridView1.DataSource = ds.Tables(0)
        GridView1.DataBind()
        con.Close()


    End Sub

    Protected Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox3.TextChanged

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        'Insert values into the database, using raw SQL and a stored procedure. note: we'll just about only be using stored procedures, so get used to them.

        con.Open()
        'da.InsertCommand = New SqlCommand("insert into tblUsers values (@name, @email, @pass)", con)
        Dim cmd As New SqlCommand
        cmd.CommandText = "hello"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = con
        cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = TextBox1.Text
        cmd.Parameters.AddWithValue("@email", SqlDbType.VarChar).Value = TextBox2.Text
        cmd.Parameters.AddWithValue("@pass", SqlDbType.VarChar).Value = TextBox3.Text

        'raw sql works correctly, just using the stored procedure.
        'da.InsertCommand = New SqlCommand("hello", con)
        'da.InsertCommand.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text
        'da.InsertCommand.Parameters.Add("@email", SqlDbType.VarChar).Value = TextBox2.Text
        'da.InsertCommand.Parameters.Add("@pass", SqlDbType.VarChar).Value = TextBox3.Text
        'da.InsertCommand.ExecuteNonQuery()
        cmd.ExecuteNonQuery()
        con.Close()

    End Sub
End Class